package hwThree1;

import edu.princeton.cs.algs4.SequentialSearchST;
import java.util.Scanner;

// 作业 3.1.1
public class GPA_Calculator {
    public static void main(String[] args) {
        // 创建 SequentialSearchST 符号表
        SequentialSearchST<String, Double> gradeToPoint = new SequentialSearchST<>();
        gradeToPoint.put("A+", 4.33);
        gradeToPoint.put("A", 4.00);
        gradeToPoint.put("A-", 3.67);
        gradeToPoint.put("B+", 3.33);
        gradeToPoint.put("B", 3.00);
        gradeToPoint.put("B-", 2.67);
        gradeToPoint.put("C+", 2.33);
        gradeToPoint.put("C", 2.00);
        gradeToPoint.put("C-", 1.67);
        gradeToPoint.put("D", 1.00);
        gradeToPoint.put("F", 0.00);

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一系列字母成绩（用空格隔开）：");

        // 读取用户输入的字母成绩
        String input = scanner.nextLine();
        String[] grades = input.split("\\s+");

        double totalPoints = 0.0;
        int count = 0;

        // 遍历输入的成绩，计算总分和数量
        for (String grade : grades) {
            if (gradeToPoint.contains(grade)) {
                totalPoints += gradeToPoint.get(grade);
                count++;
            } else {
                System.out.println("无效的成绩: " + grade);
            }
        }

        // 计算并打印 GPA
        if (count > 0) {
            double gpa = totalPoints / count;
            System.out.printf("GPA: %.2f%n", gpa);
        } else {
            System.out.println("未输入有效成绩，无法计算 GPA。");
        }

        scanner.close();
    }
}